package com.pug.zixun.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pug.zixun.bo.CourseBo;
import com.pug.zixun.bo.UserBo;
import com.pug.zixun.commons.enums.AdminUserResultEnum;
import com.pug.zixun.commons.ex.PugBusinessException;
import com.pug.zixun.commons.utils.fn.asserts.Vsserts;
import com.pug.zixun.commons.utils.pwd.MD5Util;
import com.pug.zixun.mapper.UserMapper;
import com.pug.zixun.pojo.User;
import com.pug.zixun.service.BaseService;
import com.pug.zixun.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/5/13$ 21:29$
 */

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService, BaseService {


    @Override
    public UserBo saveOrUpdateUser(User user) {
        User oldUser = this.getById(user.getId());
        if((user!=null && user.getId()==null)){
            LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(User::getTelephone,user.getTelephone());
            int count = this.count(lambdaQueryWrapper);
            if(count>0){
                throw new PugBusinessException(AdminUserResultEnum.ACCOUNT_REG_ERROR);
            }
        }
        if (Vsserts.isNotEmpty(user.getPassword())) {
            user.setPassword(MD5Util.md5slat(user.getPassword()));
        } else {
            // 把原始密码放回去
            if (oldUser != null && Vsserts.isNotEmpty(oldUser.getPassword())) {
                user.setPassword(oldUser.getPassword());
            }
        }
        this.saveOrUpdate(user);
        return tranferBo(user, UserBo.class);
    }


    @Override
    public List<UserBo> findUserList(UserVo userVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userVo.getId());
        List<User> userList = this.list(queryWrapper);
        return tranferListBo(userList, UserBo.class);
    }


    @Override
    public IPage<UserBo> pageUser(UserVo userVo) {
        // 设置分页
        Page<User> page = new Page<>(userVo.getPageNo(), userVo.getPageSize());
        // 设置条件
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(userVo.getUsername()), User::getUsername, userVo.getUsername());
        // 分页反射
        IPage<User> userIPage = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(userIPage, UserBo.class);
    }

    @Override
    public UserBo getUserById(Long userId) {
         return  tranferBo(this.getById(userId), UserBo.class);
    }


}

